<?php
namespace app\index\controller;

use think\console\Command;
use think\Controller;

class User extends Auth
{
    public function _empty($name){
        return "无效操作：".$name;
    }


    public function index()
    {
        return $this->fetch();
    }

    //我的应用
    public function myapp(){
        $keywords=input('keywords','','trim');
        if($keywords)$map[]=['order_sn','like','%'.$keywords.'%'];

        $state=input('state',-1);
        if($state>-1)$map[]=['state_pay','=',$state];

        $map=$this->condtime($map);

        $sort="id desc";
        $map[]=['uid','=',UID];
        $pagesize=12;
        $res  = db("myapp")->where($map)->order($sort)->paginate($pagesize,false,['query'=>request()->param()]);
        $list = $res->toArray()['data'];

        return view("",compact('res','list','keywords','state'));
    }

    //资金明细
    public function cash(){
        $pagesize=10;
        $map[]=['uid','=',UID];

        $map=$this->condtime($map);

        $sort="id desc";
        $res  = db("cash")->where($map)->order($sort)->paginate($pagesize,false,['query'=>request()->param()]);
        $list = $res->toArray()['data'];

        return view("",compact('res','list'));
    }

    //会员信息
    public function account(){
       $uid=UID;

        if(request()->isPost()){
            $this->editDataMethod(false,false,false,$uid);
        }

       $info=model('user')->where('id',$uid)->find();
       $this->assign('info',$info);
       return $this->fetch();
    }


    //修改密码
    public function pwd(){
        if(request()->isPost()){
            $password=input('password');
            if(!$password)$this->error('请输入密码');
            $newpwd=input('newpwd');
            if(!$newpwd)$this->error('请输入新密码');
            $repwd=input('repwd');
            if($repwd!=$newpwd)$this->error('确认码不正确');

            $object=model('user');
            $map[]=['id','=',UID];
            $map[]=['password','=',md5($password)];
            if(!$object->where($map)->find())$this->error('原密码不正确');

            $data['password']=md5($newpwd);
            if(false===$object->where(["id"=>UID])->update($data))$this->error('修改失败');
            return $this->success('修改成功');
        }
        return $this->fetch();
    }


    //应用市场
    //*+------------------------------------------------------------------------+*/

    //应用列表
    public function market(){
        $sort="sort desc";
        $map['hide_flag']=0;
        $map['state']=1;
        $pagesize=12;
        $res  = db("isgoods")->where($map)->order($sort)->paginate($pagesize,false,['query'=>request()->param()]);
        $list = $res->toArray()['data'];

        return view("",compact('res','list'));
    }


    //应用信息
    public function marketinfo(){
        $id=input('id');
        if(!$id)return;
        $map['hide_flag']=0;
        $map['state']=1;
        $map['id']=$id;
        $info=db("isgoods")->where($map)->find();
        if(!$info)return;
        $this->assign('info',$info);
        return $this->fetch();
    }

    //属性
    public function sku(){
        $gid=input('gid');
        $code=input('code','','trim');
        $map['goods_id']=$gid;
        $map['sign_id']=$code;
        $map['is_delete']=0;
        $sinfo=db('isgoods_attr')->where($map)->find();
        return json($sinfo);
    }


    //下单
    public function order(){
        $gid=input('gid');
        $map['hide_flag']=0;
        $map['state']=1;
        $map['id']=$gid;
        $info=db("isgoods")->where($map)->find();
        if(!$info)$this->error('该应用不存在或已下架');

        //对应的sku
        $attr_id=input('attr_id');
        $tmap['goods_id']=$gid;
        $tmap['id']=$attr_id;
        $tmap['is_delete']=0;
        $sinfo=db('isgoods_attr')->where($tmap)->find();
        if(!$sinfo)$this->error('该应用不存在或已下架');

        //订单信息
        $data['order_sn']=date('YmdHis').mt_rand(10000,99999);
        $data['gid']=$gid;
        $data['attr_id']=$attr_id;
        $data['uid']=UID;
        $data['gname']=$info['name'];
        $data['num']=1;
        $data['sum']=$sinfo['price'];
        $data['attr']=$sinfo['sign_str'];
        $data['time']=time();

        $id=db('isorder')->insertGetId($data);
        if(!$id)$this->error('下单成功');
        $this->success('下单成功，立即支付',url('pay',['oid'=>$id]),['cmd'=>1]);
    }


    //支付订单
    public function pay(){
        $oid=input('oid');

        if(request()->isPost()){
            $ptype=input('ptype');
            $map['id']=$oid;
            $info=db('isorder')->where($map)->find();
            if(!$info)$this->error('订单不存在');
            if(1==$info['state_pay'])$this->error('该订单已支付');

            //1、余额支付
            if(1==$ptype){
                $umap['id']=UID;
                $uinfo=db('user')->where($umap)->find();
                if($uinfo['cash']<$info['sum'])$this->error('余额不足');

                $udata['uid']=UID;
                $udata['cat']=3; //支出
                $udata['cash']=-$info['sum'];
                $udata['appid']=$oid;
                $udata['time']=time();
                $cid=db('cash')->insertGetId($udata);
                if(!$cid)$this->error('支付失败');

                db('user')->where($umap)->dec('cash',$info['sum'])->update();
                $uedata['time_pay']=time();
                $uedata['state_pay']=1;
                $uedata['paytype']=$ptype;
                db('isorder')->where($map)->update($uedata);
                return $this->success('支付成功',url('myorder'),['cmd'=>1]);
            }

            //2、微信支付
            if(2==$ptype){
               $this->error('微信支付接口未开通');
            }

            return;
        }


        $map['id']=$oid;
        $map['state_pay']=0;
        $info=db('isorder')->where($map)->find();
        $this->assign('info',$info);
        return $this->fetch();
    }


    //我的订单
    public function myorder(){
        $keywords=input('keywords','','trim');
        if($keywords)$map[]=['order_sn','like','%'.$keywords.'%'];

        $state=input('state',-1);
        if($state>-1)$map[]=['state_pay','=',$state];

        $map=$this->condtime($map);

        $sort="id desc";
        $map[]=['uid','=',UID];
        $pagesize=12;
        $res  = db("isorder")->where($map)->order($sort)->paginate($pagesize,false,['query'=>request()->param()]);
        $list = $res->toArray()['data'];

        return view("",compact('res','list','keywords','state'));
    }


    //订单详情
    public function myorderinfo(){
        $oid=input('oid');
        if(!$oid)return false;
        $map['id']=$oid;
        $map['uid']=UID;
        $info=db('isorder')->where($map)->find();
        return view('',compact('info'));
    }



    //退出登录
    public function logout(){
        $save_pre = config("common.user_login_save_pre");
        cookie($save_pre.'_auth', null);
        cookie($save_pre.'_auth_sign', null);
        session($save_pre.'_auth', null);
        session($save_pre.'_auth_sign', null);
        session('__forward__',null);
        return redirect('com/login');
    }

}
